<?php

/**
 * 验证手机号
 */
function checkMobile($mobile)
{
    return preg_match('/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/i', $mobile);
}

function carTypeTitle($type)
{
    switch ($type) {
        case '1':
            $title = '轿车';

            break;
        case '2':
            $title = 'SUV';

            break;
        case '3':
            $title = '新能源';

            break;
        case '4':
            $title = 'MPV';

            break;
        default:
            $title = '轿车';
    }

    return $title;
}

function answerTypeTitle($type)
{
    switch ($type) {
        case '1':
            $title = 'SINGLE';

            break;
        case '2':
            $title = 'MULTY';

            break;
        case '3':
            $title = 'QUESTION';

            break;
        default:
            $title = 'QUESTION';
    }

    return $title;
}

function xcwyPackageIds()
{
    return [28];
}

function jcbyPackageIds()
{
    return [29, 30];
}

function bjqPackageIds()
{
    return [31];
}

function carType()
{
    return ['1' => '轿车', '2' => 'SUV', '3' => '新能源', '4' => 'MPV'];
}

function statusSwitch()
{
    return ['1' => '开启', '2' => '关闭'];
}

function mergeSwitch()
{
    return ['1' => '合并', '2' => '不合并'];
}

function statusSwitchTitle($status)
{
    return $status == 1 ? '开启' : '关闭';
}

function usedStatusSwitchTitle($status)
{
    return $status == 1 ? '已核销' : '未核销';
}

function packageTypeTitle($type)
{
    return $type == 1 ? '销售套餐' : '售后套餐';
}


function path2url($path)
{
    $url = '';

    if ($path) {
        if (substr($path, 0, 1) == '/') {
            $url = env('QN_APP_URL').$path;
        } else {
            $url = env('QN_APP_URL').'/'.$path;
        }

    }

    return $url;
}

function pathsUrl($path)
{
    $url = [];

    if ($path) {
        foreach ($path as $key => $value) {
            if ($value) {
                if (substr($value['url'], 0, 1) == '/') {
                    $url[$key]['url'] = env('QN_APP_URL').$value['url'];
                } else {
                    $url[$key]['url'] = env('QN_APP_URL').'/'.$value['url'];
                }

            }
        }
    }

    return $url;
}

function successMessage($data = [], $message = '成功', $code = 200)
{
    return ['code' => $code, 'data' => $data, 'message' => $message];
}

function failureMessage($message = '失败', $code = 400)
{
    return ['code' => $code, 'message' => $message];
}

/**
 * 验证是否本地环境
 * @return bool
 */
function isLocalHost()
{
    if (env('APP_ENV') == 'local') {
        return true;
    } else {
        return false;
    }
}

/**
 * 获取真实IP地址
 */
function getRealIp()
{
    $ip = false;
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
        $ip = getenv("HTTP_CLIENT_IP");
    } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
        $ips = explode(", ", getenv("HTTP_X_FORWARDED_FOR"));
        if ($ip) {
            array_unshift($ips, $ip);
            $ip = false;
        }
        $ipscount = count($ips);
        for ($i = 0; $i < $ipscount; $i++) {
            if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
        $ip = getenv("REMOTE_ADDR");
    } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
        $ip = $_SERVER['REMOTE_ADDR'];
    } else {
        $ip = "unknown";
    }

    return isIp($ip) ? $ip : "unknown";
}

/**
 * 检查是否是合法的IP
 */
function isIp($ip)
{
    if (preg_match('/^((\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)(?:\.(\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)){3})$/', $ip)) {
        return true;
    } else {
        return false;
    }
}

function curlRequest($url, $data = '')
{
    $ch = curl_init();
    $header = array("Accept-Charset: utf-8");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $tmpInfo = curl_exec($ch);

    if (curl_errno($ch)) {
        return false;
    } else {
        return $tmpInfo;
    }
}